//  Created by 莫瑞权 on 2022/5/20.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFCourseManagement: NSObject {

   
    //保存进度
    @objc static func setPlay(_ roomId:NSInteger,second:NSInteger)
    {
        //所有已经保存的房间进度
        let allPlayProgress = NSMutableArray.init(array: self.getAllPlayProgress())
         
        if (allPlayProgress.count>100) {
            allPlayProgress.removeObject(at: 0)
        }

        var Index:NSInteger = 0
            
        var copyDict:NSMutableDictionary?
        
        for i in 0..<allPlayProgress.count {
        
               let dict = allPlayProgress[i] as! NSDictionary
              //找到已经存在的人
             let obj = dict["roomId"]
             if obj is NSNumber {
                 let bb = obj as! NSNumber
                
                 if (NSInteger(bb.intValue) == roomId) {
                     Index = i
                     copyDict = NSMutableDictionary.init(dictionary: dict)
                     copyDict!.setObject((roomId), forKey: "roomId" as NSCopying )
                     copyDict!.setObject((second), forKey: "second" as NSCopying )
                     break
                 }
            }
            
              
            }
            //找到了替换 //更新存在的进度
            if (copyDict != nil) {
                allPlayProgress.replaceObject(at: Index, with: copyDict as Any)
            }else{
                //新添一个进度
                let dict = NSMutableDictionary()
                dict.setObject((roomId), forKey: "roomId" as NSCopying )
                dict.setObject((second), forKey: "second" as NSCopying )
                allPlayProgress.add(dict)

            }
            
            //本地保存进度
        let userDefault = UserDefaults.standard
        userDefault.set(allPlayProgress, forKey: self.playProgressKey())
        userDefault.synchronize()

    }
//获取进度的本地列表
    static func getAllPlayProgress()->NSArray
    {
        let userDefault = UserDefaults.standard
        let obj = userDefault.object(forKey: self.playProgressKey())
        
        userDefault.synchronize()
      
        if(obj is NSArray)
        {
            return obj as! NSArray
        }
        return NSArray()
       
    }
    static func playProgressKey()->String {
         return  "TalkfunPlayProgressKey"
     }
    
    //读取回放播放的进度
    @objc static func getPlayProgress(_ roomId:NSInteger)->NSInteger
    {
        
        var second:NSInteger  = 0
        
        //所有已经保存的房间进度
        let allPlayProgress = NSMutableArray.init(array: self.getAllPlayProgress())
//        print("所有已经保存的房间进度======>",allPlayProgress as Any)
        //更新存在的进度
        for i in 0..<allPlayProgress.count {
        
            let obj = allPlayProgress[i]
            
            if obj is NSDictionary {
                let dict = obj as! NSDictionary
                
                //找到已经存在的人
               let aa =  dict["roomId"]
                if aa is NSNumber {
                   let bb = aa as! NSNumber
                    
                    if NSInteger(bb.intValue) == roomId {
                        
                        let cc =  dict["second"] as! NSNumber
                        second = NSInteger(cc.intValue)
                    }
                    
                    
                }
            
            }
        }

        return second
        
    }

    //=================================================================
    static func getAllLogoUrl()->NSArray
    {

        let obj = UserDefaults.standard.object(forKey: "TalkfunLogoUrl")
        UserDefaults.standard.synchronize()
        
        if(obj is NSArray)
        {
            return obj as! NSArray
        }
        return NSArray()
       
    }
  
   @objc static func saveLogoUrl(_ url:String,roomId:NSInteger,title:String)

    {
        
        if (url != nil && title != nil) {

            //所有已经保存的房间进度
            
            let allUrl = NSMutableArray.init(array: self.getAllLogoUrl())
    
            if (allUrl.count>100) {
                allUrl.removeObject(at: 0)
            }

            var  Index:NSInteger = 0
            
            var copyDict:NSMutableDictionary?
            for i in 0..<allUrl.count {
            
                let dict = allUrl[i] as! NSDictionary
      
                let obj = dict["roomId"]
                if obj is NSNumber {
                    let bb = obj as! NSNumber
                    //找到已经存在的人
                    if (NSInteger(bb.intValue) == roomId) {
                        Index = i
                        copyDict = NSMutableDictionary.init(dictionary: dict)
                        copyDict!.setObject(url, forKey: "url" as NSCopying)
                        copyDict!.setObject(title, forKey: "title" as NSCopying)
                        break
                    }
               }
               
                
            }
            
            //找到了替换 //更新存在的进度
            if (copyDict != nil) {
                allUrl.replaceObject(at: Index, with: copyDict as Any)
                
            }else{
                //新添一个进度
           
                copyDict = NSMutableDictionary()
                copyDict!.setObject((roomId), forKey: "roomId" as NSCopying)
                copyDict!.setObject(url, forKey: "url" as NSCopying)
                copyDict!.setObject(title, forKey: "title" as NSCopying)
                allUrl.add(copyDict!)
         
            }
            
            //本地保存进度
            let userDefault = UserDefaults.standard
            userDefault.set(allUrl, forKey: "TalkfunLogoUrl")
            userDefault.synchronize()
        
        }
    }
    //读取
  @objc static func getLogoUrl( _ roomId:NSInteger)->NSMutableDictionary
    {
        
        var copyDict:NSMutableDictionary?
        
        //所有已经保存的房间进度
        let allPlayProgress = NSMutableArray.init(array: self.getAllLogoUrl())
        
        for i in 0..<allPlayProgress.count {
        
        let dict = allPlayProgress[i] as! NSDictionary
            
        let obj = dict["roomId"]
        if obj is NSNumber {
            let bb = obj as! NSNumber
            
            if (NSInteger(bb.intValue) == roomId) {
                copyDict = NSMutableDictionary.init(dictionary: dict)
            }


        }
            
        }
        
    if copyDict != nil {
        return copyDict!
      
    }
        
    return NSMutableDictionary()
        
}
    //=================================================================
    //是否能跳过广告
   @objc static func skipAdvertising( _ roomId:NSInteger)->Bool
    {
        
        let allPlayProgress = NSMutableArray.init(array: self.getAdvertising())
        
        var skip:Bool = false
        
        for i in 0..<allPlayProgress.count
        {
            let dict = allPlayProgress[i] as! NSDictionary

            let obj = dict["roomId"]
            if obj is NSNumber {
                let bb = obj as! NSNumber
                
                if (NSInteger(bb.intValue) == roomId) {
                    skip = true
                }


            }
           
        }
       
        return skip;
    }
  @objc static func saveSkip( _ roomId:NSInteger)
    {
        let allPlayProgress = NSMutableArray.init(array: self.getAdvertising())

        if (allPlayProgress.count>100) {
            allPlayProgress.removeObject(at: 0)
        }

        //   [arr replaceObjectAtIndex:索引 withObject:替换的元素];
        var  Index:NSInteger = 0
        
        var copyDict:NSMutableDictionary?
        for i in 0..<allPlayProgress.count {
        
            let dict = allPlayProgress[i] as! NSDictionary
  
            let obj = dict["roomId"]
            if obj is NSNumber {
                let bb = obj as! NSNumber
                //找到已经存在的人
                if (NSInteger(bb.intValue) == roomId) {
                    
                    Index = i
                    
                    copyDict = NSMutableDictionary.init(dictionary: dict)

                    break;
                }
                
            }
            
        }

        if (copyDict != nil) {
            allPlayProgress.replaceObject(at: Index, with: copyDict as Any)
            
        }else{
            //新添一个进度
       
            copyDict = NSMutableDictionary()
            
            copyDict!.setObject((roomId), forKey: "roomId" as NSCopying)
     
            allPlayProgress.add(copyDict!)
     
        }
        
        //本地保存进度
        let userDefault = UserDefaults.standard
        userDefault.set(allPlayProgress, forKey: self.TalkfunAdvertising())
        userDefault.synchronize()
    }
    static func getAdvertising()->NSArray
    {
        let obj = UserDefaults.standard.object(forKey:self.TalkfunAdvertising())
        UserDefaults.standard.synchronize()
        
        if(obj is NSArray)
        {
            return obj as! NSArray
        }
    
        return NSArray()
    }
    static func TalkfunAdvertising()->String {
         return  "TalkfunAdvertising"
     }
}
